package com.atguigu.juc.study.chapter10.tl;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2022年06月17日 23:31
 */
public class ThreadLocalDemo {

    public static void main(String[] args) {

        House house = new House();

        for (int i = 1; i <= 5; i++) {

            new Thread(() -> {
                int size = new Random().nextInt(5) + 1;
                // System.out.println(size);

                try {
                    for (int j = 1; j <= size; j++) {
                        house.saleHouse();
                        house.saleVolumeByThreadLocal();
                    }

                    System.out.println(Thread.currentThread().getName() + "\t" + "号销售卖出：" + house.getSaleVolume().get());
                } finally {
                    house.getSaleVolume().remove();
                }
            }, String.valueOf(i)).start();
        }

        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + "\t" + "共计卖出多少套：" + house.getSaleCount());
    }
}
